package pmsjc.dpi.psaudavel;

import java.util.ArrayList;
import java.util.HashMap;

import pmsjc.dpi.psaudavel.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class colesterolActivity extends Activity{

	private stru_pessoa pesColesterol;
	public  BuscaWS busca;
    private Boolean ColesterolAlterado = false;

	public void PreencheTabela(){
		//Tabela: Press‹o Arterial
		ListView list = (ListView) findViewById(R.id.lstcolesterol);
   	    		
    	ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    	HashMap<String, String> map = new HashMap<String, String>();
    	map.put("situacao", "Normal");
    	map.put("valor", "até 199");
    	mylist.add(map);

    	map = new HashMap<String, String>();
    	map.put("situacao", "Limítrofe");
    	map.put("valor", "de 200 a 229");
    	mylist.add(map);
    	
    	map = new HashMap<String, String>();
    	map.put("situacao", "Aumentado");
    	map.put("valor", ">=230");
    	mylist.add(map);
    	
    	ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.rows,
    	            new String[] {"situacao", "valor"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL});
    	list.setAdapter(mSchedule);
    	
	}
		
	public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.colesterol);
	        
	        InicializaDados();
	        Log.i("Colesterol - act", "create");
	}
		
	public void ResultadoColesterol(int mostra){
		LinearLayout lyt = (LinearLayout) findViewById(R.id.resultadocol);
		lyt.setVisibility(mostra);
	}
	
	public void CalculoColesterol(){
					TextView txtcolesterol = (TextView) findViewById(R.id.txtcolesterol); 	
					TextView txtresColesterol = (TextView) findViewById(R.id.txtResColesterol);                
	                double res = Double.valueOf(String.valueOf(txtcolesterol.getText()));
	    
	            	Log.i("Click - Colesterol", "clicou!" + pesColesterol.pessoa_nome);
	                
	                String informacao = "Ainda não calculado";
                              
	            	  if (res < 200){
	            		  informacao = "NORMAL";       
	            	  }else if (res >= 200 && res < 230 ){
	            		  informacao = "LIMITROFE";   
	            	  }else if (res >= 230 ){
	            		  informacao = "AUMENTADO";   
	            	  }
            	         
            		Log.i("Click - Colesterol", "resultado:" + res);
            		Log.i("Click - Colesterol", "info:" + informacao);
            		
            		ResultadoColesterol(View.VISIBLE);
               		
            		txtresColesterol.setText(String.valueOf(informacao));              
	}
	
	public void InicializaDados(){
		Log.i("Click - Colesterol", "InicializaDados" );
		TextView txtcolesterol = (TextView) findViewById(R.id.txtcolesterol);

		 TextView txtResultado = (TextView) findViewById(R.id.txtresultcol);
   		 txtResultado.setText("");
   		 
   		TextView txtdatasistema = (TextView) findViewById(R.id.txtdatasistemaColesterol);      
   		 
   		/*
   			<achou>string</achou>
         	<pCPF>long</pCPF>
         	<pColesterol>long</pColesterol>
         	<data_exame>string</data_exame>
         	<data_sistema>string</data_sistema>
         */
		
		busca = new BuscaWS();
		
		txtcolesterol.setText(busca.BuscaColesterol(PSaudavelActivity.pessoa.pessoa_cpf));
		ColesterolAlterado =false;

	    ResultadoColesterol(View.INVISIBLE);
	}
		
	@Override
	protected void onResume() {
		Log.i("Colesterol - act", "resume");
		Button btnSalvar = (Button) findViewById(R.id.btncolesterol);
	   
	    pesColesterol  = PSaudavelActivity.pessoa;
	    
	    InicializaDados();	    
	   
	
		PreencheTabela();
        btnSalvar.setOnClickListener(new OnClickListener() {
		 	
		 
			public void onClick(View v) {
				  Log.e("colesterol alterado:", ColesterolAlterado.toString());
				if (ColesterolAlterado){
					TextView txtcolesterol = (TextView) findViewById(R.id.txtcolesterol);
					String[] res = busca.GravaColesterol(pesColesterol.pessoa_cpf, txtcolesterol.getText().toString() );
            		if (res[0].contains("S")){
	            		//Aparece o resultado
            			 TextView txtResultado = (TextView) findViewById(R.id.txtresultcol);
            	   		 txtResultado.setText("Colesterol gravado com sucesso!");
            			CalculoColesterol();
	               	}else{
               			//msg erro
	               		Toast toast= Toast.makeText(getApplicationContext(),"Erro ao salvar colesterol!", Toast.LENGTH_SHORT);  							
	   				 toast.setGravity(Gravity.TOP, 0, 70);
	   				 toast.show();
	               	}
            		ColesterolAlterado = false;
        		}else{        	
        			 TextView txtResultado = (TextView) findViewById(R.id.txtresultcol);
        	   		 txtResultado.setText("Colesterol gravado com sucesso!");
        			CalculoColesterol();
        		}
			}
		});
      
        TextView txtcolesterol = (TextView) findViewById(R.id.txtcolesterol);
        
      
        
        txtcolesterol.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				ColesterolAlterado = true;
				
			}
	
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
		super.onResume();
	}

}
